package cn.demo.service.serviceimpl;

import cn.demo.Util.AgeByBirthday;
import cn.demo.Util.Page;
import cn.demo.dao.PermissionDao;
import cn.demo.dao.RoleDao;
import cn.demo.dao.UserInfoDao;
import cn.demo.entity.Permission;
import cn.demo.entity.Role;
import cn.demo.entity.UserInfo;
import cn.demo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author weizhongli
 * @Date 2020/3/22
 * @Version v1.0
 * @Description
 */
@Service("userService")
public class UserInfoServiceImpl implements UserInfoService {

    /*植入dao层接口  重写该方法*/
    /*防止报空*/
    @Autowired
    private UserInfoDao userdao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    /*
    * 登录
    * */
    public UserInfo Login(UserInfo userInfo) {
        return userdao.Login(userInfo);
    }

    /*
    * 分页展示
    * */
    public Page userList(String userName, int PageIndex, int PageSize) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("PageIndex", PageIndex * PageSize);
        map.put("PageSize", PageSize);
        map.put("userName", userName);
        Page page = new Page();
        page.setPageIndex(PageIndex);
        page.setPageSize(PageSize);
        int i = userdao.countuserlist(userName);
        page.setTotalRecords(i);
        int totalPages = page.getTotalRecords() % page.getPageSize() == 0 ? page.getTotalRecords() / page.getPageSize() : page.getTotalRecords() / page.getPageSize() + 1;
        page.setPageTotalpages(totalPages);
        List<UserInfo> list = userdao.userLists(map);
        for (UserInfo item : list) {
            Date birthday = item.getBirthday();
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = myFormat.format(birthday);
            int age = AgeByBirthday.getagee(format);
            item.setAge(age);
            if (item.getGender() == 1) {
                item.setGendername("男");
            }
            if (item.getGender() == 2) {
                item.setGendername("女");
            }
        }
        page.setList(list);
        return page;
    }


    public int countuserlist(String userName) {
        return userdao.countuserlist(userName);
    }

    /*
    * 添加用户
    * */
    public int insertUser(UserInfo userInfo) {
        return userdao.insertUser(userInfo);
    }

    public UserInfo chuanuser(int id) {
        return userdao.chuanuser(id);
    }

    /*
    * 更新用户
    * */
    public int updateuser(UserInfo userInfo) {
        return userdao.updateuser(userInfo);
    }

    /*
    * 删除用户
    * */
    public int deleteuser(int id) {
        return userdao.deleteuser(id);
    }

    public List<UserInfo> selectUser() {
        return userdao.selectUser();
    }

    /*
     * 根据用户id查询用户信息
     * */
    public UserInfo findByUsername(String useCode) {
        //根据用户名获取用户信息
        UserInfo userInfo = userdao.findByUsername(useCode);
        //根据用户id获取照相关角色信息
        Role role = roleDao.findByUserId(userInfo.getId());
        userInfo.setRole(role);
        //根据角色Id 获取关联的权限信息
        Set<Permission> permissionset = permissionDao.findByRoleId(role.getId());
        role.setPermissions(permissionset);
        return userInfo;
    }
}
